﻿using Microsoft.AspNetCore.Mvc;
using YDT_OrderService.Applications.DTOs;
using YDT_OrderService.Applications.Interfaces;

namespace YDT_OrderService.Api.Controllers
{
    /// <summary>
    /// 订单控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class OrdersController : ControllerBase
    {
        private readonly IOrderService _orderService;

        public OrdersController(IOrderService orderService)
        {
            _orderService = orderService;
        }

        [HttpGet]
        public async Task<ActionResult<IEnumerable<OrderDto>>> GetOrders()
        {
            var orders = await _orderService.GetAllOrdersAsync();
            return Ok(orders);
        }

        [HttpGet("{id:guid}")]
        public async Task<ActionResult<OrderDto>> GetOrder(Guid id)
        {
            var order = await _orderService.GetOrderByIdAsync(id);
            if (order == null)
            {
                return NotFound();
            }
            return Ok(order);
        }

        [HttpGet("sn/{orderSn}")]
        public async Task<ActionResult<OrderDto>> GetOrderBySn(string orderSn)
        {
            var order = await _orderService.GetOrderByOrderSnAsync(orderSn);
            if (order == null)
            {
                return NotFound();
            }
            return Ok(order);
        }

        [HttpGet("user/{userId:guid}")]
        public async Task<ActionResult<IEnumerable<OrderDto>>> GetOrdersByUser(Guid userId)
        {
            var orders = await _orderService.GetOrdersByUserIdAsync(userId);
            return Ok(orders);
        }

        [HttpPost]
        public async Task<ActionResult<OrderDto>> CreateOrder(CreateOrderDto createOrderDto)
        {
            var order = await _orderService.CreateOrderAsync(createOrderDto);
            return CreatedAtAction(nameof(GetOrder), new { id = order.Id }, order);
        }

        [HttpPut("{id:guid}")]
        public async Task<ActionResult<OrderDto>> UpdateOrder(Guid id, UpdateOrderDto updateOrderDto)
        {
            var order = await _orderService.UpdateOrderAsync(id, updateOrderDto);
            if (order == null)
            {
                return NotFound();
            }
            return Ok(order);
        }

        [HttpDelete("{id:guid}")]
        public async Task<ActionResult> DeleteOrder(Guid id)
        {
            var result = await _orderService.DeleteOrderAsync(id);
            if (!result)
            {
                return NotFound();
            }
            return NoContent();
        }

        [HttpPatch("{id:guid}/pay")]
        public async Task<ActionResult<OrderDto>> MarkAsPaid(Guid id)
        {
            var order = await _orderService.MarkOrderAsPaidAsync(id);
            if (order == null)
            {
                return NotFound();
            }
            return Ok(order);
        }

        [HttpPatch("{id:guid}/ship")]
        public async Task<ActionResult<OrderDto>> MarkAsShipped(Guid id)
        {
            var order = await _orderService.MarkOrderAsShippedAsync(id);
            if (order == null)
            {
                return NotFound();
            }
            return Ok(order);
        }

        [HttpPatch("{id:guid}/complete")]
        public async Task<ActionResult<OrderDto>> MarkAsCompleted(Guid id)
        {
            var order = await _orderService.MarkOrderAsCompletedAsync(id);
            if (order == null)
            {
                return NotFound();
            }
            return Ok(order);
        }

        [HttpPatch("{id:guid}/cancel")]
        public async Task<ActionResult<OrderDto>> CancelOrder(Guid id)
        {
            var order = await _orderService.CancelOrderAsync(id);
            if (order == null)
            {
                return NotFound();
            }
            return Ok(order);
        }
    }
}
